/**
  ******************************************************************************
  * @file    flash.c
  * @author  xEndless
  * @version V1.0.0
  * @date    2020-03-27
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */ 
/* Includes ------------------------------------------------------------------*/
#include "flash.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/ 

/* Private function prototypes -----------------------------------------------*/   

/* Private functions ---------------------------------------------------------*/
void FLASH_Read(uint32_t read_addr, uint8_t *read_buf, uint16_t length)
{
    uint16_t loop;
    
    for(loop = 0; loop < length; loop++)
    {
        read_buf[loop] = *(uint32_t*)read_addr;
        read_addr++;
    }
}

void FLASH_Write(uint32_t TypeProgram, uint32_t write_addr, uint8_t *write_buf, uint16_t length)
{
    uint16_t loop;
    uint16_t len;
    
    /* Unlock the Flash to enable the flash control register access *************/
    HAL_FLASH_Unlock();
    /* half word 16-bits */
    if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
    {
        
    }
    /* word 32-bits */
    else
    {
        
    }
    /* Lock the Flash to disable the flash control register access (recommended
        to protect the FLASH memory against possible unwanted operation) *********/
    HAL_FLASH_Lock();
}

uint32_t FLASH_Erase(uint32_t page_addr, uint16_t page_num)
{
    uint32_t PageError = 0;
    FLASH_EraseInitTypeDef pEraseInit;

    /* Unlock the Flash to enable the flash control register access *************/ 
    HAL_FLASH_Unlock();

    pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
    pEraseInit.PageAddress = page_addr;
    pEraseInit.Banks = FLASH_BANK_1;
    pEraseInit.NbPages = page_num;
    HAL_FLASHEx_Erase(&pEraseInit, &PageError);

    /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
    HAL_FLASH_Lock();
    
    return PageError;
}
/*******************************END OF FILE************************************/
